package com.galaxy.util;


import net.coobird.thumbnailator.Thumbnails;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;


/**
 * 图片缩放处理
 *
 * @author anjie
 * @version 1.0
 * @date 2020/8/21 10:09
 * @email anjie@032t.com
 */
public class PUtil {

    /**
     * 对照片进行像素调整
     *
     * @param originPicture 原始的照片输入流
     * @return 修改过后的
     */
    public InputStream fixPicture(InputStream originPicture, int w, int h, String suffix) {

        boolean b = suffix.startsWith(".");
        if (b)
            suffix = suffix.substring(suffix.lastIndexOf(".") + 1);

        InputStream picture = null;

        BufferedImage tmpImage;
        try {

            BufferedImage read = ImageIO.read(originPicture);

            tmpImage = Thumbnails.of(read)
                    .size(w, h)
//                    .keepAspectRatio(true)
                    .asBufferedImage();

            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(tmpImage, suffix, os);
            picture = new ByteArrayInputStream(os.toByteArray());

        } catch (IOException e) {
            e.printStackTrace();
        }

        return picture;
    }



}
